package View;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.math.BigDecimal;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;

import Model.Computation;

import net.miginfocom.swing.MigLayout;

public class OutputTable extends JPanel{
	

    private DefaultTableModel bisectionModel;
    private JLabel bisectionLabel;
    private JTable bisectionTable;
    private JScrollPane bisectionScroll;
    String[] bisectionColumnNames = new String[] {"x0", "x1", "y0","y1","x2","y2","|x0-x1|"};
    private DefaultTableModel newtonModel;
    private JLabel newtonLabel;
    private JTable newtonTable;
    private JScrollPane newtonScroll;
    String[] newtonColumnNames = new String[] {"x", "f(x)", "f'(x)"};
    
    private JTabbedPane tableTabbedPane;
	
	public OutputTable(){
		setLayout(new MigLayout());
		
		initComponent();
		addComponent();
	}
	
	private class RowHeaderRenderer extends JLabel implements ListCellRenderer {
   	 
        RowHeaderRenderer(JTable table) {
 
            setOpaque(true);
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));
            setHorizontalAlignment(CENTER);
            setForeground(new Color(50,50,50));
            setFont(new Font("Arial", Font.BOLD, 10));
    	}
 
        public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
 
            setText((value == null) ? "" : value.toString());
            return this;
        }
    }

	private void initComponent() {
        bisectionLabel = new JLabel("Bisection Method Table");
        bisectionTable = new JTable(bisectionModel);
        bisectionTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        bisectionTable.setFocusable(false);
        bisectionTable.setRowSelectionAllowed(false);
        bisectionTable.setEnabled(false);
        
        bisectionScroll = new JScrollPane(bisectionTable);
        bisectionScroll.setPreferredSize(new Dimension(500, 300));
        
        newtonLabel = new JLabel("Newton Method Table");
        newtonTable = new JTable(newtonModel);
        newtonTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        newtonTable.setFocusable(false);
        newtonTable.setRowSelectionAllowed(false);
        newtonTable.setEnabled(false);
        
        newtonScroll = new JScrollPane(newtonTable);
        newtonScroll.setPreferredSize(new Dimension(500, 300));
        
        tableTabbedPane = new JTabbedPane();
        tableTabbedPane.add("Bisection Table", bisectionScroll);
        tableTabbedPane.add("Newton Table", newtonScroll);
		
	}

	private void addComponent() {
		add(tableTabbedPane);
		
	}
	
	public void updateData(Computation computation){
		newtonModel = new DefaultTableModel(newtonColumnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        
        for(int i = 0; i < computation.getFirstColumn().size(); i++){
        	
        	Double first = computation.getFirstColumn().get(i);
        	Double second = computation.getSecondColumn().get(i);
        	Double third = computation.getThirdColumn().get(i);
        	String x;
        	String fx;
        	String fprimex;
        	
        	if(!first.isNaN())
        		x = new BigDecimal(String.valueOf(computation.getFirstColumn().get(i))).setScale(6, BigDecimal.ROUND_HALF_UP).toString();     		
        	else
        		x = String.valueOf(first);
        	
        	if(!second.isNaN())
        		fx = new BigDecimal(String.valueOf(computation.getSecondColumn().get(i))).setScale(6, BigDecimal.ROUND_HALF_UP).toString();
        	else
        		fx = String.valueOf(second);
        	
        	if(!third.isNaN())
        		fprimex = new BigDecimal(String.valueOf(computation.getSecondColumn().get(i))).setScale(6, BigDecimal.ROUND_HALF_UP).toString();
        	else
        		fprimex = String.valueOf(third);
        	
    
        	Object[] data = {x, fx, fprimex};		                        
        	
        	newtonModel.addRow(data);
        }
        newtonTable.setModel(newtonModel);
        
        bisectionModel = new DefaultTableModel(bisectionColumnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        
        for(int i = 0; i < computation.getColumnOne().size(); i++){
        	Object[] data2 = {new BigDecimal(String.valueOf(computation.getColumnOne().get(i))).setScale(6, BigDecimal.ROUND_HALF_UP).toString(),
        			         new BigDecimal(String.valueOf(computation.getColumnTwo().get(i))).setScale(6, BigDecimal.ROUND_HALF_UP).toString(),
        			         new BigDecimal(String.valueOf(computation.getColumnThree().get(i))).setScale(6, BigDecimal.ROUND_HALF_UP).toString(),
        			         new BigDecimal(String.valueOf(computation.getColumnFour().get(i))).setScale(6, BigDecimal.ROUND_HALF_UP).toString(),
        			         new BigDecimal(String.valueOf(computation.getColumnFive().get(i))).setScale(6, BigDecimal.ROUND_HALF_UP).toString(),
        			         new BigDecimal(String.valueOf(computation.getColumnSix().get(i))).setScale(6, BigDecimal.ROUND_HALF_UP).toString(),
        			         new BigDecimal(String.valueOf(computation.getColumnSeven().get(i))).setScale(6, BigDecimal.ROUND_HALF_UP).toString()};		                        
        	
        	bisectionModel.addRow(data2);
        }
        bisectionTable.setModel(bisectionModel);
        
	}

}
